package com.allwees.core.mongo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MongoBaseDaoImpl<T> implements MongoBaseDao<T> {

    protected Class<T> entityClass;

    @Autowired
    protected MongoTemplate mongoTemplate;

    @SuppressWarnings("unchecked")
    public MongoBaseDaoImpl() {
        Type genType = this.getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        entityClass = (Class<T>) params[0];
    }

    @Override
    public List<T> getAllObjects() {
        return getMongoTemplate().findAll(entityClass);
    }

    @Override
    public int saveObject(T object) {
        int res = -1;
        try {
            getMongoTemplate().insert(object);
            res = 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public int saveObjectList(List<T> list) {
        int res = -1;
        try {
            getMongoTemplate().insertAll(list);
            res = 0;
        } catch (Exception e) {
        }
        return res;
    }

    @Override
    public T findById(String id) {
        return getMongoTemplate().findOne(new Query(Criteria.where("id").is(id)), entityClass);
    }

    @Override
    public List<T> getOnes(List<String> ids) {

        return getMongoTemplate().find(new Query(Criteria.where("id").in(ids)), entityClass);
    }

    @Override
    public void update(T object) {
        getMongoTemplate().save(object);
    }

    @Override
    public int deleteObject(String id) {
        int res = -1;
        try {
            getMongoTemplate().remove(new Query(Criteria.where("id").is(id)), entityClass);
            res = 0;
        } catch (Exception e) {
        }

        return res;
    }

    @Override
    public int deleteObjectByIds(List<String> list) {
        int res = -1;
        try {
            getMongoTemplate().remove(new Query(Criteria.where("id").in(list)), entityClass);
            res = 0;
        } catch (Exception e) {
        }

        return res;
    }

    @Override
    public void deleteAndSave(String id, T object) {
        this.deleteObject(id);
        this.saveObject(object);
    }


    public MongoTemplate getMongoTemplate() {
        return this.mongoTemplate;
    }

    public Query getQuery(Map<String, Object> map) {

        Query query = new Query();
        Set<Entry<String, Object>> entrys = map.entrySet();
        for (Entry<String, Object> e : entrys) {
            String key = e.getKey();
            Object value = e.getValue();
            query.addCriteria(Criteria.where(key).is(value));
        }
        return query;
    }

    public Query getQuery(String key, Object value) {

        Query query = new Query(Criteria.where(key).is(value));
        return query;
    }


    @Override
    public boolean exists(String id) {
        return getMongoTemplate().exists(getQuery("id", id), entityClass);
    }


    @Override
    public long count() {
        return 0L;
    }
}
